package com.vd.wmt.ui;

import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.border.TitledBorder;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.vd.wmt.model.Status;
import com.vd.wmt.model.WageDetail;
import com.vd.wmt.parse.PropParse;
import com.vd.wmt.process.LoadProcess;
import com.vd.wmt.process.ProcessCallBack;
import com.vd.wmt.process.SendProcess;

/**
 * @desc UI
 * @author tudongyun
 * 
 */
public class WmtUI extends JFrame {
	private static Log log = LogFactory.getLog(WmtUI.class);

	private static final long serialVersionUID = 1L;

	private JPanel jPanelConfig;
	private JScrollPane jScrollPane;
	private JTable jTable;
	private JTextField jTextUser;
	private JPasswordField jPasswordField;

	private JButton jButtonLoad;
	private JButton jButtonSend;
	private WageTableModel wageTableModel;
	private SendProcess sendProcess;
	private LoadProcess loadProcess;

	public WmtUI(SendProcess sendProcess, LoadProcess loadProcess) {
		this.sendProcess = sendProcess;
		this.loadProcess = loadProcess;
		initComponents();
		initConfig();
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setExtendedState(MAXIMIZED_BOTH);
		setTitle("工资条发送工具  2.0");
	}

	private void initComponents() {
		jPanelConfig = new JPanel();
		jPanelConfig.setBorder(new TitledBorder("系统设置"));
		GridBagLayout frameLayout = new GridBagLayout();
		this.getContentPane().setLayout(frameLayout);
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.fill = GridBagConstraints.BOTH;
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.weightx = 1;
		gbc.weighty = 0.15;
		this.getContentPane().add(jPanelConfig, gbc);
		jScrollPane = new JScrollPane();
		jScrollPane.setBorder(new TitledBorder("工资明细"));
		this.getContentPane().setLayout(frameLayout);
		GridBagConstraints gbc2 = new GridBagConstraints();
		gbc2.fill = GridBagConstraints.BOTH;
		gbc2.gridx = 0;
		gbc2.gridy = 1;
		gbc2.weightx = 1;
		gbc2.weighty = 0.85;
		this.getContentPane().add(jScrollPane, gbc2);
		// JTable
		wageTableModel = new WageTableModel();
		jTable = new JTable();
		jTable.setModel(wageTableModel);
		jScrollPane.setViewportView(jTable);
		// Config
		jPanelConfig.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 5));
		JLabel jLabelUser = new JLabel("用户名:");
		jPanelConfig.add(jLabelUser);
		jTextUser = new JTextField(15);
		jPanelConfig.add(jTextUser);
		JLabel jLabelPwd = new JLabel("密   码:");
		jPanelConfig.add(jLabelPwd);
		jPasswordField = new JPasswordField(15);
		jPanelConfig.add(jPasswordField);
		jButtonLoad = new JButton("  导入Excel  ");
		jPanelConfig.add(jButtonLoad);
		jButtonSend = new JButton("  发送邮件  ");
		jPanelConfig.add(jButtonSend);
		pack();
	}

	private void initConfig() {
		jTextUser.setText(PropParse.getInstance().getValue("vd.send.user"));
		jPasswordField.setText(PropParse.getInstance().getValue("vd.send.passwd"));
		jButtonLoad.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (sendProcess.getStatus().equals(Status.SENDING)) {
					showTipMsg("邮件正在发送中，请稍后。");
					return;
				}
				FileDialog fileDialog = new FileDialog(WmtUI.this, "工资表excel");
				fileDialog.setMode(FileDialog.LOAD);
				fileDialog.setVisible(true);
				// String file =
				// fileDialog.getDirectory()+File.separator+fileDialog.getFile();
				String file = fileDialog.getFile();
				log.info("get file:" + file);
				if (file == null || file.isEmpty()) {
					showErrMsg("未找到excel文件，请确认路径是否正确!");
					return;
				}
				file = fileDialog.getDirectory() + File.separator + file;
				List<WageDetail> list = loadProcess.process(file);
				if (loadProcess.getStatus().equals(Status.SUCC)) {
					wageTableModel.getList().clear();
					wageTableModel.AddWageDetail(list);
				} else {
					showErrMsg("系统错误:" + loadProcess.getError());
				}
			}
		});
		jButtonSend.addActionListener(new ActionListener() {
			@SuppressWarnings("deprecation")
			@Override
			public void actionPerformed(ActionEvent e) {
				sendProcess.SendMsg(wageTableModel.getList(), callBack, jTextUser.getText().trim(), jPasswordField
						.getText().trim());
			}
		});
	}

	public void showErrMsg(String msg) {
		JOptionPane.showMessageDialog(this, "系统错误：\n" + msg);
	}

	public void showTipMsg(String msg) {
		JOptionPane.showMessageDialog(this, msg);
	}
	private ErrorTableCellRenderer errRender = new ErrorTableCellRenderer();
	private ProcessCallBack callBack = new ProcessCallBack(){

		@Override
		public void exec(WageDetail wage) {
			wageTableModel.updateSendStatus(wage.getIndex()-1, wage.getStatus());
			if(wage.getStatus().equals(Status.FAILD)){
				errRender.addRow(wage.getIndex()-1);
			}
			jTable.getColumn(jTable.getColumnName(0)).setCellRenderer(errRender);
		}

		@Override
		public void tipMsg(String msg) {
			showTipMsg(msg);
		}};
}
